<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    destroyOnClose
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="!!model.id" label="ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['id']" ></a-input>
        </a-form-item>
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item label="广告主" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['advertiser']" placeholder="请输入广告主"></a-input>
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag
            type="list" v-decorator="['type', validatorRules.type]"
            @change="val => model.type=Number(val)"
            :trigger-change="true" dictCode="ads_ad_type" placeholder="请选择类型"/>
        </a-form-item>
        <a-form-item label="尺寸类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['sizeTypeName']"
            :trigger-change="true"
            org-fields="name,width,height"
            dest-fields="sizeTypeName,width,height"
            code="ads-size-type"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="宽 px" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="!!model.sizeTypeName" v-decorator="['width', validatorRules.width]" placeholder="请输入宽 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="高 px" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="!!model.sizeTypeName" v-decorator="['height', validatorRules.height]" placeholder="请输入高 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="点击链接" v-show="[1,2,3].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clickUrl', validatorRules.clickUrl]" placeholder="请输入点击链接"></a-input>
        </a-form-item>
        <a-form-item label="目标窗口" v-show="[1,2,3].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['targetWindow', validatorRules.targetWindow]" placeholder="请输入：_blank， _parent, _self, _top，或framename"></a-input>
        </a-form-item>

        <a-form-item label="文件来源" v-show="[2,3].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fileSourceType', validatorRules.fileSourceType]"
                             @change="val => model.fileSourceType=Number(val)"
                             :trigger-change="true" dictCode="ads_file_source_type" placeholder="请选择文件来源"/>
        </a-form-item>

        <!--注意：需设置modal为destroyOnClose， 否则edit时，如果与上次显示的分支不同，会导致from.setFieldsValue设置不上! -->
        <a-form-item v-if="model.type===1"
                     label="广告文字" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入广告文字"/>
        </a-form-item>
        <a-form-item v-else-if="model.type===4"
                     label="html&js" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入HTML和JavaScript"/>
        </a-form-item>
        <a-form-item v-else-if="model.type===5"
                     label="广告位代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入第三方广告位代码（一段式）"/>
        </a-form-item>
        <!-- [2,3].includes(model.type) -->
        <a-form-item v-else-if="model.fileSourceType===1"
                     label="上传文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['content', validatorRules.content]" :trigger-change="true"></j-upload>
        </a-form-item>
        <!-- [2,3].includes(model.type) -->
        <a-form-item v-else-if="model.fileSourceType===2"
                     label="远程文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入文件url"/>
        </a-form-item>

        <a-form-item v-show="model.type===4" label="广告元素" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['contentExt']" rows="4" placeholder="请输入广告元素"/>
        </a-form-item>

        <a-form-item v-show="model.type===1" label="展示样式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['style']" rows="4" placeholder="请输入展示样式"/>
        </a-form-item>

        <a-form-item label="启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['enabled', validatorRules.enabled]" :trigger-change="true" dictCode="yn" placeholder="请选择启用"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remark']" ></a-input>
        </a-form-item>

        <a-form-item label="创建者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createBy']" ></a-input>
        </a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="修改者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateBy']"></a-input>
        </a-form-item>
        <a-form-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateTime']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: "AdModal",
    components: {
      JDate,
      JDictSelectTag,
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          type: {
            rules: [
              { required: true, message: '请输入类型!'},
            ]
          },
          width: {
            rules: [
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          height: {
            rules: [
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          clickUrl: {
            rules: [
              { pattern: /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/, message: '请输入正确的网址!'},
            ]
          },
          targetWindow: {
            rules: [
              { required: true, message: '请输入目标窗口!'},
            ],
            initialValue: '_blank'
          },
          fileSourceType: {
            rules: [
              { required: true, message: '请输入文件来源!'},
            ],
            initialValue: 1
          },
          content: {
            rules: [
              { required: true, message: '请输入内容!'},
            ]
          },
          enabled: {
            rules: [
              { required: true, message: '请输入启用!'},
            ],
            initialValue: 1
          },
        },
        url: {
          add: "/ad/add",
          edit: "/ad/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        //模板中v-if、v-show等用到的数据的初始值，需在本处设置（v-decorator中的initialValue一开始未同步到model）
        this.edit({targetWindow:'_blank', fileSourceType:1, enabled:1});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'id','name','advertiser','type','sizeTypeName','width','height','clickUrl','targetWindow','fileSourceType','content','contentExt','style','enabled','remark','createBy','createTime','updateBy','updateTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'id','name','advertiser','type','sizeTypeName','width','height','clickUrl','targetWindow','fileSourceType','content','contentExt','style','enabled','remark','createBy','createTime','updateBy','updateTime'))
        this.model.sizeTypeName = row.sizeTypeName;
      },


    }
  }
</script>

<style lang="less" scoped>
  .ant-row.ant-form-item {
    margin-bottom: 12px;
  }
</style>